<template>
  <el-card>
    <el-button
      @click="$router.push('/role/add')"
      type="primary"
      icon="el-icon-circle-plus-outline"
    >添加</el-button>
    <el-divider />
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="id" label="ID" width="180"></el-table-column>
      <el-table-column prop="rolename" label="名称" width="180"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.status==1">正常</el-tag>
          <el-tag type="danger" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            @click="$router.push(`/role/${scope.row.id}`)"
            circle
            type="primary"
            icon="el-icon-edit"
            size="mini"
          ></el-button>
          <el-button circle type="warning" size="mini" icon="el-icon-view"></el-button>
          <el-button
            @click="del(scope.row.id)"
            circle
            type="danger"
            size="mini"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      list: []
    };
  },
  created() {
    this.getRoleList();
  },
  methods: {
    // 实现角色的删除
    del(id) {
      this.$myConfirm(async ()=>{
        const res=await this.$http.post(`/api/roledelete`,{id});
        if(res.code!=200){
          this.$error('删除失败');
        }
      });
    },
    getRoleList() {
      this.$http.get("/api/rolelist").then(res => {
        if (res.code == 200) {
          this.list = res.list;
        }
      });
    }
  }
};
</script>

<style>
</style>